﻿using AI_Tank.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AI_Tank.Game;
using System.Threading;

namespace PlayGame
{
    public partial class PlayerClient : Form
    {
        private ServerConnector socketConnector;


        private static string tankMoveDir;
        private static string bulletMoveDir;
        private static bool fireSuperBullet;
        private Thread OnTimeSendAction;

        public string TankMoveDir { get => tankMoveDir; }
        public string BulletMoveDir { get => bulletMoveDir; }
        public bool FireSuperBullet { get => fireSuperBullet; }

        public PlayerClient()
        {

            InitializeComponent();
            DealRoundData(new ServerConnectorEventArgs("cao"));    //测试定时传输Action数据
            AppHotKey.RegKey(Handle, 0, AppHotKey.KeyModifiers.None,  Keys.Up); 
            AppHotKey.RegKey(Handle, 1, AppHotKey.KeyModifiers.None, Keys.Down); 
            AppHotKey.RegKey(Handle, 2, AppHotKey.KeyModifiers.None, Keys.Left); 
            AppHotKey.RegKey(Handle, 3, AppHotKey.KeyModifiers.None, Keys.Right); 
            AppHotKey.RegKey(Handle, 4, AppHotKey.KeyModifiers.None, Keys.W); 
            AppHotKey.RegKey(Handle, 5, AppHotKey.KeyModifiers.None, Keys.S); 
            AppHotKey.RegKey(Handle, 6, AppHotKey.KeyModifiers.None, Keys.A); 
            AppHotKey.RegKey(Handle, 7, AppHotKey.KeyModifiers.None, Keys.D);
            AppHotKey.RegKey(Handle, 8, AppHotKey.KeyModifiers.Ctrl, Keys.W);
            AppHotKey.RegKey(Handle, 9, AppHotKey.KeyModifiers.Ctrl, Keys.S);
            AppHotKey.RegKey(Handle, 10,AppHotKey.KeyModifiers.Ctrl, Keys.A);
            AppHotKey.RegKey(Handle, 11,AppHotKey.KeyModifiers.Ctrl, Keys.D);
        }



        private void button2_Click(object sender, EventArgs e)
        {
            socketConnector = ServerConnector.Connect(textBox10.Text, Int32.Parse(textBox11.Text));
            socketConnector.OnReceiveData += SocketConnector_OnReceiveData1;
            socketConnector.OnSocketException += SocketConnector_OnSocketException1;
        }

        private void SocketConnector_OnSocketException1(object sender, ServerConnectorEventArgs e)
        {
            label29.Text = "Sorry,Socket网络连接异常!";
        }

        private void SocketConnector_OnReceiveData1(object sender, ServerConnectorEventArgs e)
        {
            MessageType mt = ServerMessageTypePaser.Parse(e.Msg);
            switch (mt)
            {
                case MessageType.Leg_start:
                    textBox12.Text += "半场开始"; DealLegStartData(e);
                    break;
                case MessageType.Round:
                    textBox12.Text += "新回合"; DealRoundData(e); fireSuperBullet = false;
                    break;
                case MessageType.Leg_end:
                    textBox12.Text += "半场结束\n\n";
                    break;
                case MessageType.Game_over:
                    textBox12.Text += "游戏结束";
                    break;
                case MessageType.Nothing:
                    textBox12.Text += "接受的什么鬼东西！！！";
                    break;

            }
        }

        private void textBox11_KeyPress(object sender, KeyPressEventArgs e)
        {
            char kc = e.KeyChar;
            if ((kc < 48 || kc > 57) && kc != 8)
                e.Handled = true;
        }

        private void DealRoundData(ServerConnectorEventArgs e)
        {
            OnTimeSendAction = new Thread(new ThreadStart(OnSendActionTimer));
            OnTimeSendAction.Start();
            return;                                            //测试定时传输信息
            ParseRoundData prd = new ParseRoundData(e.Msg);
            RoundData roundDataObj = prd.GetRoundDataObj();
            label17.Text = roundDataObj.Teams[0].Point.ToString();
            label18.Text = roundDataObj.Teams[1].Point.ToString();
            label27.Text = roundDataObj.Teams[0].RemainLife.ToString();
            label28.Text = roundDataObj.Teams[1].RemainLife.ToString();
            label24.Text = roundDataObj.RoundID.ToString();
        }
        System.Threading.Timer timer;
        private void OnSendActionTimer()
        {
            TimerCallback TimeIsUp = SendAction;
            timer = new System.Threading.Timer(TimeIsUp,null,10000,15000);
        }

        private void SendAction(Object state)
        {
            MessageBox.Show("发送一条数据啦");
            timer.Dispose();

        }

        private void DealLegStartData(ServerConnectorEventArgs e)
        {
            LegStartData pld = ParseLegStartData.GetMapData(e.Msg);

            label15.Text = pld.TeamA_ID.ToString();
            label20.Text = pld.TeamB_ID.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void PlayerClient_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show(e.KeyCode.ToString());
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {

            TextBox textBoxObj = (TextBox)sender;

            Keys bulletUpKey = Keys.None;
            Keys bulletDownKey = Keys.None;
            Keys bulletLeftKey = Keys.None;
            Keys bulletRightKey = Keys.None;
            switch (textBoxObj.Tag.ToString())
            {
                case "tankUp": if (!AppHotKey.RegKey(Handle, 0, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) textBoxObj.Text = ""; MessageBox.Show("成功重置热键！"); break;
                case "tankDown": if (!AppHotKey.RegKey(Handle, 1, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) textBoxObj.Text = ""; MessageBox.Show("成功重置热键！"); break;
                case "tankLeft": if (!AppHotKey.RegKey(Handle, 2, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) textBoxObj.Text = ""; MessageBox.Show("成功重置热键！"); break;
                case "tankRight": if (!AppHotKey.RegKey(Handle, 3, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) textBoxObj.Text = ""; MessageBox.Show("成功重置热键！"); break;
                case "bulletUp": if (!AppHotKey.RegKey(Handle, 4, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) {textBoxObj.Text = ""; MessageBox.Show("成功重置热键！"); break; } bulletUpKey = (Keys)e.KeyChar; break;
                case "bulletDown": if (!AppHotKey.RegKey(Handle, 5, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) { textBoxObj.Text = ""; break; } MessageBox.Show("成功重置热键！"); bulletDownKey = (Keys)e.KeyChar; break;
                case "bulletLeft": if (!AppHotKey.RegKey(Handle, 6, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) { textBoxObj.Text = ""; break; } MessageBox.Show("成功重置热键！"); bulletLeftKey = (Keys)e.KeyChar; break;
                case "bulletRight": if (!AppHotKey.RegKey(Handle, 7, AppHotKey.KeyModifiers.None, (Keys)e.KeyChar)) { textBoxObj.Text = ""; break; } MessageBox.Show("成功重置热键！"); bulletRightKey = (Keys)e.KeyChar; break;
                case "fireSuperBullet": RegSuperBullet(); break;
                default:
                    break;
            }
            void RegSuperBullet()
            {
                if (bulletUpKey == Keys.None || bulletDownKey == Keys.None || bulletLeftKey == Keys.None || bulletRightKey == Keys.None)
                {
                    MessageBox.Show("请先设定子弹发射方向热键");
                    textBoxObj.Text = "";
                    return;
                }
                AppHotKey.KeyModifiers km = AppHotKey.KeyModifiers.Ctrl;
                switch ((Keys)e.KeyChar)
                {
                    case Keys.Alt: km = AppHotKey.KeyModifiers.Alt; break;
                    case Keys.Control: km = AppHotKey.KeyModifiers.Ctrl; break;
                    case Keys.LWin: km = AppHotKey.KeyModifiers.WindowsKey; break;
                    default: MessageBox.Show("目前只支持alt,control和左Win键!"); break;
                }
                AppHotKey.RegKey(Handle, 8, km, bulletUpKey);
                AppHotKey.RegKey(Handle, 9, km, bulletDownKey);
                AppHotKey.RegKey(Handle, 10, km, bulletLeftKey);
                AppHotKey.RegKey(Handle, 11, km, bulletRightKey);
            }
            button1.Select();
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = "";
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键   
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 0:
                            tankMoveDir = "up";
                            MessageBox.Show("坦克上移");
                            break;
                        case 1:
                            tankMoveDir = "down";
                            MessageBox.Show("坦克下移");
                            break;
                        case 2:
                            tankMoveDir = "left";
                            MessageBox.Show("坦克左移");
                            break;
                        case 3:
                            tankMoveDir = "right";
                            MessageBox.Show("坦克右移");
                            break;
                        case 4:
                            bulletMoveDir = "up";
                            MessageBox.Show("向上发射子弹");
                            break;
                        case 5:
                            bulletMoveDir = "down";
                            MessageBox.Show("向下发射子弹");
                            break;
                        case 6:
                            bulletMoveDir = "left";
                            MessageBox.Show("向左发射子弹");
                            break;
                        case 7:
                            bulletMoveDir = "right";
                            MessageBox.Show("向右发射子弹");
                            break;
                        case 8:
                            fireSuperBullet = true;
                            MessageBox.Show("向上发射超级子弹");
                            break;
                        case 9:
                            fireSuperBullet = true;
                            MessageBox.Show("向下发射超级子弹");
                            break;
                        case 10:
                            fireSuperBullet = true;
                            MessageBox.Show("向左发射超级子弹");
                            break;
                        case 11:
                            fireSuperBullet = true;
                            MessageBox.Show("向右发射超级子弹");
                            break;
                        default:
                            MessageBox.Show("热键错误！");
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }

    }
}
